package com.yingda.utils;

import android.content.Context;
import android.util.Log;

import com.hjq.toast.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.yingda.net.Api;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class SendMessageUtils {
    private static SendMessageUtils instance;
    private SendMessageUtils(){}
    public static final String TAG="SendMessageUtils";

    public static SendMessageUtils getInstance(){
        if (instance == null){
            synchronized (SendMessageUtils.class){
                if (instance == null){
                    instance = new SendMessageUtils();
                }
            }
        }
        return instance;
    }

    public void setMessage(String sender, String messageBody, String newPhone){
        String url = Api.sendMessage;
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                //短信内容
                .addFormDataPart("txt", messageBody)
                //发送者
                .addFormDataPart("from", sender)
                //本人手机号
                .addFormDataPart("n", newPhone)
                //所属id
                .addFormDataPart("cusId", "1")
                        .build();
        OkGo.<String>post(url)
                .upRequestBody(requestBody)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ToastUtils.show("发送消息成功");
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.show("发送消息失败");
                    }
                });
    }

    //接受手机号
    public void getPhone(Context context, String newPhone){
        String url = Api.Accept_phone_number;
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                //余额
                .addFormDataPart("spend", "0.00")
                 //1、在线 2、掉线
                .addFormDataPart("sts", "1")
                //设备编号
                .addFormDataPart("imei", QZUtils.getDeviceId(context))
                //本人手机号 todo 要修改
                .addFormDataPart("n", newPhone)
                //所属id
                .addFormDataPart("cusId", "1")
                .build();
        OkGo.<String>post(url)
                .upRequestBody(requestBody)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ToastUtils.show("接收手机号成功");
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Log.i(TAG, "onError: "+response);
                        ToastUtils.show("接收手机号失败");
                    }
                });
    }
}
